#ifndef USERDATA_H
#define USERDATA_H

#include <QString>
#include <memory>
#include <QJsonArray>
#include <vector>
#include <QJsonObject>

/******************************************************************************
 *
 * @file       userdata.h
 * @brief      Function: 内存缓存用户数据节点汇总
 *
 * @author     DustWind丶
 * @date       2025/06/03
 * @history
 *****************************************************************************/
class SearchInfo {
public:
    SearchInfo(int uid, QString name, QString nick, QString desc, int sex, QString icon);
    ~SearchInfo();

    int     _uid;
    QString _name;
    QString _nick;
    QString _desc;
    int     _sex;
    QString _icon;
};

class AddFriendApply {
public:
    AddFriendApply(int from_uid, QString name, QString desc, QString icon, QString nick, int sex);
    ~AddFriendApply();

    int     _from_uid;
    QString _name;
    QString _desc;
    QString _icon;
    QString _nick;
    int     _sex;
};

struct ApplyInfo {
    ApplyInfo(int uid, QString name, QString desc, QString icon, QString nick, int sex, int status):
        _uid(uid),
        _name(name),
        _desc(desc),
        _icon(icon),
        _nick(nick),
        _sex(sex),
        _status(status){}

    ApplyInfo(std::shared_ptr<AddFriendApply> addinfo):
        _uid(addinfo->_from_uid),
        _name(addinfo->_name),
        _desc(addinfo->_desc),
        _icon(addinfo->_icon),
        _nick(addinfo->_nick),
        _sex(addinfo->_sex),
        _status(0){}

    void SetIcon(QString head)
    {
        _icon = head;
    }

    int     _uid;
    QString _name;
    QString _desc;
    QString _icon;
    QString _nick;
    int     _sex;
    int     _status;
};

struct AuthInfo {
    AuthInfo(int uid, QString name, QString nick, QString icon, int sex):
        _uid(uid),
        _name(name),
        _nick(nick),
        _icon(icon),
        _sex(sex){}

    int     _uid;
    QString _name;
    QString _nick;
    QString _icon;
    int     _sex;
};

struct AuthRsp {
    AuthRsp(int peer_uid, QString peer_name, QString peer_nick, QString peer_icon, int peer_sex):
        _uid(peer_uid),
        _name(peer_name),
        _nick(peer_nick),
        _icon(peer_icon),
        _sex(peer_sex){}

    int     _uid;
    QString _name;
    QString _nick;
    QString _icon;
    int     _sex;
};

struct TextChatData; //前置声明

// 好友信息结构
struct FriendInfo {
    FriendInfo(int uid, QString name, QString nick, QString icon,
               int sex, QString desc, QString back, QString last_msg=""):
        _uid(uid),
        _name(name),
        _nick(nick),
        _icon(icon),
        _sex(sex),
        _desc(desc),
        _back(back),
        _last_msg(last_msg){}

    //好友信息（对端认证信息）
    FriendInfo(std::shared_ptr<AuthInfo> auth_info):
        _uid(auth_info->_uid),
        _name(auth_info->_name),
        _nick(auth_info->_nick),
        _icon(auth_info->_icon),
        _sex(auth_info->_sex){}

    //好友信息（本端认证回包）
    FriendInfo(std::shared_ptr<AuthRsp> auth_rsp):
        _uid(auth_rsp->_uid),
        _name(auth_rsp->_name),
        _nick(auth_rsp->_nick),
        _icon(auth_rsp->_icon),
        _sex(auth_rsp->_sex){}

    void AppendChatMsgs(const std::vector<std::shared_ptr<TextChatData>> text_vec);

    int     _uid;
    QString _name;
    QString _nick;
    QString _icon;
    int     _sex;
    QString _desc;
    QString _back;
    QString _last_msg;
    std::vector<std::shared_ptr<TextChatData>> _chat_msgs; //聊天记录
};

//用户信息结构
struct UserInfo {
    //默认构造
    UserInfo(int uid, QString name, QString icon):
        _uid(uid),
        _name(name),
        _nick(_name),
        _icon(icon),
        _sex(0),
        _desc(""),
        _last_msg(""){}

    //扩展参数构造
    UserInfo(int uid, QString name, QString nick, QString icon, int sex, QString last_msg = "", QString desc=""):
        _uid(uid),
        _name(name),
        _nick(nick),
        _icon(icon),
        _sex(sex),
        _desc(desc),
        _last_msg(last_msg){}

    //认证信息（发送给对方的信息）
    UserInfo(std::shared_ptr<AuthInfo> auth_info):
        _uid(auth_info->_uid),
        _name(auth_info->_name),
        _nick(auth_info->_nick),
        _icon(auth_info->_icon),
        _sex(auth_info->_sex),
        _desc(""),
        _last_msg(""){}

    //认证回复（从服务器获取回复）
    UserInfo(std::shared_ptr<AuthRsp> auth_rsp):
        _uid(auth_rsp->_uid),
        _name(auth_rsp->_name),
        _nick(auth_rsp->_nick),
        _icon(auth_rsp->_icon),
        _sex(auth_rsp->_sex),
        _last_msg(""){}

    //搜索信息
    UserInfo(std::shared_ptr<SearchInfo> search_info):
        _uid(search_info->_uid),
        _name(search_info->_name),
        _nick(search_info->_nick),
        _icon(search_info->_icon),
        _sex(search_info->_sex),
        _last_msg(""){}

    //好友信息
    UserInfo(std::shared_ptr<FriendInfo> friend_info):
        _uid(friend_info->_uid),
        _name(friend_info->_name),
        _nick(friend_info->_nick),
        _icon(friend_info->_icon),
        _sex(friend_info->_sex),
        _last_msg(""){
            _chat_msgs = friend_info->_chat_msgs;
        }

    int     _uid;
    QString _name;
    QString _nick;
    QString _icon;
    int     _sex;
    QString _desc;
    QString _last_msg;
    std::vector<std::shared_ptr<TextChatData>> _chat_msgs; //聊天记录
};

// 单条聊天信息
struct TextChatData{
    TextChatData(QString msg_id, QString msg_content, int fromuid, int touid):
        _msg_id(msg_id),
        _msg_content(msg_content),
        _from_uid(fromuid),
        _to_uid(touid){}

    QString _msg_id;
    QString _msg_content;
    int     _from_uid;
    int     _to_uid;
};

// 文本聊天消息包（封包用于与服务器交互传数据），可能会包含多条聊天信息
struct TextChatMsg{
    TextChatMsg(int fromuid, int touid, QJsonArray arrays):
        _from_uid(fromuid),
        _to_uid(touid)
    {
        for(auto  msg_data : arrays)
        {
            auto msg_obj = msg_data.toObject();
            auto content = msg_obj["content"].toString();
            auto msgid = msg_obj["msgid"].toString();
            auto msg_ptr = std::make_shared<TextChatData>(msgid, content,fromuid, touid);
            _chat_msgs.push_back(msg_ptr);
        }
    }

    int _from_uid;
    int _to_uid;
    std::vector<std::shared_ptr<TextChatData>> _chat_msgs; //聊天记录
};

#endif // USERDATA_H
